<template>
  <div class="bankAdd">
    <div>
      <jc-button
        :btn-arr="['reload', 'save', 'sub', 'remove', 'pass', 'list']"
        :disab-arr="[isDisabled && 'save', 'close', 'cancel', 'copy', 'com1', 'com2', 'using', 'freeze']"
        :list="'/bankList'"
        @refresh="handelDetail"
        @save="save"
        @submit="save(1)"
        @noSubmit="submit(2)"
        @audit="audit(1)"
        @noAudit="audit(2)"
        @del="remove"
      />
    </div>
    <el-form :model="proValue" size="mini" label-width="100px">
      <el-form-item :label="$t('market.state')">
        <el-tag :type="proValue.stateColor">{{ proValue.fdocumentstatus }}</el-tag>
      </el-form-item>
      <!--      <el-form-item label="创建组织">-->
      <!--        <el-select v-model.trim="proValue.fcreateorgId" :disabled="isDisabled">-->
      <!--          <el-option-->
      <!--            v-for="option in teamList"-->
      <!--            :key="option.fid"-->
      <!--            :label="option.fname"-->
      <!--            :value="option.fid"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="使用组织">-->
      <!--        <el-select v-model.trim="proValue.fuseorgId" :disabled="isDisabled">-->
      <!--          <el-option-->
      <!--            v-for="option in teamList"-->
      <!--            :key="option.fid"-->
      <!--            :label="option.fname"-->
      <!--            :value="option.fid"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item :label="$t('market.accountname1')">
        <el-input v-model="proValue.faccountname" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item :label="$t('market.bankcode')">
        <el-input v-model="proValue.faccount" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item :label="$t('market.bankDeposit')">
        <el-select v-model.trim="proValue.faccountbanknameId" filterable :disabled="isDisabled">
          <el-option
            v-for="option in account1"
            :key="option.fid"
            :label="option.fname"
            :value="option.fid"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('market.accountattribute')">
        <el-select v-model.trim="proValue.faccountattributeId" filterable :disabled="isDisabled">
          <el-option
            v-for="option in account2"
            :key="option.fid"
            :label="option.fname"
            :value="option.fid"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('market.accounttype')">
        <el-select v-model.trim="proValue.faccounttypeId" filterable :disabled="isDisabled">
          <el-option
            v-for="option in account3"
            :key="option.fid"
            :label="option.fname"
            :value="option.fid"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('market.currency2')">
        <el-input v-model="proValue.fcurrencyname" :disabled="isDisabled">
          <i
            slot="suffix"
            class="el-input__icon el-icon-search"
            @click="currencyVisiblit=true"
          />
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('market.upstockmode')">
        <el-select v-model.trim="proValue.fupstockmodeId" filterable :disabled="isDisabled">
          <el-option
            v-for="option in account4"
            :key="option.fid"
            :label="option.fname"
            :value="option.fid"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('market.upstocknum')">
        <el-input-number v-model="proValue.fupstocknum" controls-position="right" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item :label="$t('market.retainedbalance')">
        <el-input-number v-model="proValue.fretainedbalance" controls-position="right" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item :label="$t('market.minimumupstocknum')">
        <el-input-number v-model="proValue.fminimumupstocknum" controls-position="right" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item :label="$t('market.note')">
        <el-input v-model="proValue.fremark" :disabled="isDisabled" />
      </el-form-item>
    </el-form>
    <!--币别-->
    <currency v-if="currencyVisiblit" @currency="currencyData" />
  </div>
</template>

<script>
import bility from '@/mixin/Operability'
import { queryViewItem } from '@/api/marketManage/marketOrder'
import {
  deleteBankAccount,
  noreviewBankAccount,
  nosubmitBankAccount,
  queryTFinBankAccountDetails, reviewBankAccount,
  submitBankAccount,
  updateBankAccount
} from '@/api/financial/teller'
import jcButton from '@/components/Button'
import { notoMxBmina, toMxBmina } from '@/components/ToMxamineState'

export default {
  components: {
    jcButton,
    currency: () => import('@/views/market/marketManage/createMarkerOrder/components/currency')
  },
  mixins: [bility],
  data() {
    return {
      isDisabled: false,
      currencyVisiblit: false,
      proValue: {},
      account1: [], // 开户行
      account2: [], // 账户收支属xing
      account3: [], // 账号类型
      account4: [] // 上划方式
    }
  },
  created() {
    if (this.$route.params.id) {
      this.handelDetail()
    }
    this.getForm()
    this.getForm2()
    this.getForm3()
    this.getForm4()
  },
  methods: {
    handelDetail() {
      const fbankaccountId = this.$route.params.id
      queryTFinBankAccountDetails({ fbankaccountId }).then(res => {
        if (res.code === 0) {
          this.proValue = {
            ...toMxBmina(res.data),
            faccountattributeId: Number(res.data.faccountattributeId),
            faccountbanknameId: Number(res.data.faccountbanknameId),
            faccounttypeId: Number(res.data.faccounttypeId),
            fupstockmodeId: Number(res.data.fupstockmodeId)
          }
          this.isDisabled = this.proValue.fdocumentstatus === this.$t('market.state2') || this.proValue.fdocumentstatus === this.$t('market.state1')
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 保存
    save(ev) {
      const DATA = {
        ...notoMxBmina(this.proValue)
      }
      updateBankAccount(DATA).then(res => {
        if (res.code === 0) {
          ev === 1 ? this.submit(1) : this.handelDetail()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 开户银行数据
    async getForm() {
      await queryViewItem({ viewName: 'SX_FIN_BANK' }).then(res => {
        this.account1 = res.data.list
      })
    },
    // 账户收支属xing
    async getForm2() {
      await queryViewItem({ viewName: 'SX_V_ARAE' }).then(res => {
        this.account2 = res.data.list
      })
    },
    // 账号类型
    async getForm3() {
      await queryViewItem({ viewName: 'SX_FIN_ACCOUNTTYPE' }).then(res => {
        this.account3 = res.data.list
      })
    },
    // 上划方式
    async getForm4() {
      await queryViewItem({ viewName: 'SX_FIN_UPMODE' }).then(res => {
        this.account4 = res.data.list
      })
    },
    // 币别
    currencyData(item) {
      if (item.fsettleCurrId || item.fcurrencyId) {
        this.proValue.fcurrencyname = item.fsettleCurrIdName || item.fname
        this.proValue.fcurrencyId = item.fsettleCurrId || item.fcurrencyId
      }
      this.currencyVisiblit = false
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: [this.proValue.faccount] }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handelDetail()
        this.selectVal = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    submit(ev) {
      const api = ev === 1 ? submitBankAccount : nosubmitBankAccount
      this.toExamineFun(api)
    },
    // 审核
    audit(ev) {
      const api = ev === 1 ? reviewBankAccount : noreviewBankAccount
      this.toExamineFun(api)
    },
    // 删除
    remove() {
      this.$confirm('确定要删除该订单么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const api = deleteBankAccount
        const DATA = { Numbers: [this.proValue.faccount] }
        api(DATA).then(res => {
          if (res.code !== 0) {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
            return
          }
          this.innerHTM = ''
          res.data.forEach(res => {
            this.innerHTM += '<div>' + res + '</div>'
          })
          this.$notify.warning({
            title: '提示',
            message: this.innerHTM,
            duration: 3000, offset: 50,
            dangerouslyUseHTMLString: true
          })
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: '/proceedList' })
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.bankAdd {
  @include listBom;
  background: #fff;
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    margin-top: 5px;
    //display: flex;
    flex-wrap: wrap;
    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-bottom: 10px;
      .el-input {
        width: 180px;
      }
    }
  }
}
</style>

